package com.woniu.carrent.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.woniu.carrent.mapper.*;
import com.woniu.carrent.pojo.*;
import com.woniu.carrent.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@SaCheckLogin
@RestController
@RequestMapping("/tcardateil")
@CrossOrigin
public class TCardateilController {
    @Autowired
    private TCarMapper tCarMapper;
    @Autowired
    private TCardetailMapper tCardetailMapper;
    @Autowired
    private TGearMapper tGearMapper;
    @Autowired
    private TEnergeMapper tEnergeMapper;
    @Autowired
    private TCarphotoMapper tCarphotoMapper;
    /**
     * 新增车辆详情
     * @param tcardetail
     */
    @RequestMapping("/insertCarDateil")
    public ResultMessage insertCarDateil(@RequestBody TCardetail tcardetail){
        //根据carid查询车辆价格
        TCar tcar = tCarMapper.selectById(tcardetail.getCarid());
        tcardetail.setPrice(tcar.getPrice());
        tcardetail.setIsdeleted(0);
        tcardetail.setIsonline(1);
        // 插入车辆详情
        int flag = tCardetailMapper.insert(tcardetail);
        // 获取插入后的cdid
        Integer cdid = tcardetail.getCdid();
        // 插入车辆照片
        List<TCarphoto> photoList = tcardetail.getPhotos();
        if (photoList != null && !photoList.isEmpty()) {
            for (TCarphoto tCarphoto : photoList) {
                // 设置照片关联的车辆详情ID
                tCarphoto.setCdid(cdid);
                tCarphotoMapper.insert(tCarphoto);
            }
        }
        if (flag > 0) {
            // 返回插入成功的消息，并携带生成的cdid
            return ResultMessage.success("新增成功", 200, cdid);
        }
        return ResultMessage.fail("新增失败");
    }

    /**
     * 获取车辆能源类型
     *
     */
    @GetMapping("/getEnerge")
    public ResultMessage getEnerge(){
        List<TEnerge> list = tEnergeMapper.selectList(null);
        return ResultMessage.success(list);
    }
    /**
     * 获取车辆变速箱类型
     *
     */
    @GetMapping("/getTransmission")
    public ResultMessage getTransmission(){
        List<TGear> list = tGearMapper.selectList(null);
        return ResultMessage.success(list);
    }
}
